<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WeakSet</title>
</head>
<body>
    <h1>WeakSet</h1>
    <h2>WeakSet的成员只能是对象，且都是弱引用</h2>

    <script>
        // WeakSet基本用法
        const ws = new WeakSet();
        const obj1 = {};
        const obj2 = {};
        ws.add(obj1);
        ws.add(obj2);

        console.log(ws.has(obj1)); // true
        ws.delete(obj1);
        console.log(ws.has(obj1)); // false

        // WeakSet的用途：储存DOM节点
        const nodes = new WeakSet();
        nodes.add(document.body);

        // 典型应用：标记某个对象已被处理
        const processed = new WeakSet();
        function process(obj) {
            if (processed.has(obj)) {
                return; // 已处理过
            }
            // 处理
            processed.add(obj);
        }
    </script>
</body>
</html> 